define(["jquery", "template", "request"], ($, template, req) => {
    class NavRepeat{
        constructor() {
            this.init();
        }
        init() {
            // 获取页面数据并渲染
            this.pageInit()
                // 页面js效果
                .then(() => {
                    this.jsInit();
                })

            
        }
        pageInit() {
            return req.secondNavData()
                .then((res) => {
                    if (res.code === 200) {
                        this.dataNavList = res.data;
                        // console.log(this.dataNavList);
                        return req.hotWordsData();
                    }
                })
                .then((res) => {
                    return new Promise((resolve) => {
                        if (res.code === 200) {
                            this.dataHotWords = res.data;
                            // console.log(this.dataHotWords);
                            this.dataHotWordsList = this.dataHotWords.hot;
                            this.dataRecommendList = this.dataHotWords.recommend.pc
                            // console.log(this.dataHotWordsList);
                            resolve();
                        }
                    })
                })
                .then(() => {
                    return new Promise((resolve) => {
                        // console.log(this.dataRecommendList)
                        $(".navBox").load("/html/modules/navRepeat.html", () => {
                            // 导航栏标题
                            const htmlStr1 = template("navTitleTemplate", {data : this.dataNavList});
                            // console.log(htmlStr1)
                            $("#navTitleTemplateBox").html(htmlStr1)
                            // 搜索框常驻推荐词
                            const htmlStr2 = template("recommendTemplate", {data : this.dataRecommendList});
                            // console.log(htmlStr2);
                            $("#recommendTemplateBox").html(htmlStr2)
                            // 搜索栏提示框
                            const htmlStr3 = template("hotWordsTemplate", {data : this.dataHotWordsList});
                            // console.log(htmlStr3);
                            $("#hotWordsTemplateBox").html(htmlStr3)

                            resolve();
                        });
                    })
                })

        }
        jsInit() {
            // 导航栏搜索框效果
            // console.log($("#nav"))
            $("#nav .rightSearch form input")
                .focus(function() {
                    // console.log($(this));
                    $(this)
                        .attr("placeholder", "请输入搜索的商品")
                        .parent()
                        .next()
                        .css("display", "none")
                        .next()
                        .css("display", "flex")
                        .css("z-index", "10");
                })
                .blur(function() {
                    $(this)
                        .attr("placeholder", "")
                        .parent()
                        .next()
                        .css("display", "block")
                        .next()
                        .css("display", "none")
                })
            
            // 导航栏标题指定active
            let typeObj = {
                142 : "家用电器",
                150 : "手机配件",
                151 : "手机",
                157 : "服饰箱包",
                158 : "美妆个护",
                218 : "TNT显示屏",
                321 : "抖音文创",
                555 : "家居生活",
                625 : "抖音热销"
            }
            let urlStr = location.href;
            if (urlStr.match(/\?type=\d{3}/)) {
                let typeId = urlStr.match(/\?type=\d{3}/)[0].match(/\d{3}/)[0];
                // console.log(typeId);
                let typeStr = typeObj[typeId];
                // console.log(typeStr);
                $("#nav .left li")
                .removeClass("active")
                .find(`a:contains(${typeStr})`)
                .parent()
                .addClass("active")
            } else{
                $("#nav .left li")
                .removeClass("active")
                .first()
                .addClass("active")

            }

        }
    }
    return new NavRepeat();
})
//  placeholder="请输入搜索的商品"